﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Home.MobileSales.Common.Components;
using Home.MobileSales.Common.Entities;
using Home.MobileSales.Sales.Interface;
using Home.MobileSales.Sales.Presenter;


namespace Home.MobileSales.Sales.View
{

    public partial class SettingsForm : Form, ISettingsView
    {
        public SettingsForm()
        {
            InitializeComponent();
            new SettingsPresenter(this);
        }

        private void TextBox_GotFocus(object sender, EventArgs e)
        {
            inputPanel.Enabled = true;
        }

        private void TextBox_LostFocus(object sender, EventArgs e)
        {
            inputPanel.Enabled = false;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            SaveChanges(null, null);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            CancelChanges(null, null);
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;

        }

        private string selectPath()
        {
            OpenFileDialogEx openFileDialog = new OpenFileDialogEx(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), true);
            openFileDialog.Text = "Вкажіть шлях";
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return openFileDialog.SelectedFullName;
            }
            return null;
        }

        private string selectFile(string caption)
        {
            OpenFileDialogEx openFileDialog = new OpenFileDialogEx(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase), "*.sdf", false);
            openFileDialog.Text = caption;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return openFileDialog.SelectedFullName;
            }
            return null;
        }

        private void pathDatabaseButton_Click(object sender, EventArgs e)
        {
            string path = selectFile("Вкажіть шлях до БД");
            pathDatabaseTextBox.Text = path != null ? path : string.Empty;

        }

        private void importPathButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            pathImportTextBox.Text = path != null ? path : string.Empty;
        }

        private void exportPathButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            pathExportTextBox.Text = path != null ? path : string.Empty;
        }

        #region ISettingsForm Members

        public string CompanyName
        {
            get
            {
                return companyNameTextBox.Text;
            }
            set
            {
                agentNameTextBox.Text = value;
            }
        }

        public string AgentName
        {
            get
            {
                return agentNameTextBox.Text;
            }
            set
            {
                agentNameTextBox.Text = value;
            }
        }


        public string DbPath
        {
            get
            {
                return pathDatabaseTextBox.Text;
            }
            set
            {
                pathDatabaseTextBox.Text = value;
            }
        }

        public string DbPassword
        {
            get
            {
                return passwordDatabasetextBox.Text;
            }
            set
            {
                passwordDatabasetextBox.Text = value;
            }
        }
        public string ImportPath
        {
            get
            {
                return pathImportTextBox.Text;
            }
            set
            {
                pathImportTextBox.Text = value;
            }
        }

        public string ExportPath
        {
            get
            {
                return pathExportTextBox.Text;
            }
            set
            {
                pathExportTextBox.Text = value;
            }
        }

        public bool IsSaveFile
        {
            get
            {
                return isSaveCheckBox.Checked;
            }
            set
            {
                isSaveCheckBox.Checked = value;
            }
        }

        public bool VisibleNullGoods
        {
            get
            {
                return visibleNullGoodsCheckBox.Checked;
            }
            set
            {
                visibleNullGoodsCheckBox.Checked = value;
            }
        }

        public bool CheckRemainingGoods
        {
            get
            {
                return checkRemainingGoodsCheckBox.Checked;
            }
            set
            {
                checkRemainingGoodsCheckBox.Checked = value;
            }
        }

        public bool IsVanSales
        {
            get
            {
                return vanSalesCheckBox.Checked;
            }
            set
            {
                vanSalesCheckBox.Checked = value;
            }
        }

        public string FtpUrl
        {
            get
            {
                return ftpUrlTextBox.Text;
            }
            set
            {
                ftpUrlTextBox.Text = value;
            }
        }

        public int FtpPort
        {
            get
            {
                return (int)ftpPortNumericUpDown.Value;
            }
            set
            {
                ftpPortNumericUpDown.Value = value;
            }
        }

        public string FtpUser
        {
            get
            {
                return ftpLoginTextBox.Text;
            }
            set
            {
                ftpLoginTextBox.Text = value;
            }
        }

        public string FtpPassword
        {
            get
            {
                return ftpPasswordTextBox.Text;
            }
            set
            {
                ftpPasswordTextBox.Text = value;
            }
        }

        public bool ftpIsPasiveMode
        {
            get
            {
                return ftpPasivemodeCheckBox.Checked;
            }
            set
            {
                ftpPasivemodeCheckBox.Checked = value;
            }
        }

        public string FtpImportPath
        {
            get { return ftpImportPathTextBox.Text; }
            set { ftpImportPathTextBox.Text = value; }
        }

        public string FtpExportPath
        {
            get { return ftpExportPathTextBox.Text; }
            set { ftpExportPathTextBox.Text = value; }
        }

        public List<Sklad> sklads
        {
            set
            {
                skladComboBox.DataSource = value;
                skladComboBox.DisplayMember = "NameSklad";
                skladComboBox.ValueMember = "IdSklad";
            }
        }

        public int CurrentSklad
        {
            get
            {
                return (int)skladComboBox.SelectedValue;
            }
            set
            {
                skladComboBox.SelectedValue = value;
            }
        }

        public bool AutoGenerateNumber
        {
            get { return autoGenerateDocNumbercheckBox.Checked; }
            set { autoGenerateDocNumbercheckBox.Checked = value; }
        }

        public int NumberDocument
        {
            get
            {
                return (int)numberNumericUpDown.Value;
            }
            set { numberNumericUpDown.Value = value; }
        }

        public bool EnableGps
        {
            get
            {
                return enableGpsCheckBox.Checked;
            }
            set
            {
                enableGpsCheckBox.Checked = value;
            }
        }

        public string GpsTestData
        {
            set
            {
                gpsDataLabel.Text = value;
            }
        }

        public string PrintPath
        {
            get { return pathPrintTextBox.Text; }
            set { pathPrintTextBox.Text = value; }
        }
        public string ExecuteProgramPrint
        {
            get { return execProgramPathTextBox.Text; }
            set { execProgramPathTextBox.Text = value; }
        }

        public event EventHandler<EventArgs> TestGps;

        public event EventHandler<EventArgs> SaveChanges;

        public event EventHandler<EventArgs> CancelChanges;

        #endregion

        private void vanSalesCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            checkRemainingGoodsCheckBox.Checked = vanSalesCheckBox.Checked;
            checkRemainingGoodsCheckBox.Enabled = !vanSalesCheckBox.Checked;
        }

        private void orderTabPage_Click(object sender, EventArgs e)
        {

        }

        private void autoGenerateDocNumbercheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            label6.Enabled = autoGenerateDocNumbercheckBox.Checked;
            numberNumericUpDown.Enabled = autoGenerateDocNumbercheckBox.Checked;
        }

        private void TestGPS_Click(object sender, EventArgs e)
        {
            TestGps(null, null);
        }

        private void isExecProgramCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            if (isExecProgramCheckBox.Checked)
            {
                execProgramPathTextBox.Enabled = true;
            }
            else
            {
                execProgramPathTextBox.Text = string.Empty;
                execProgramPathTextBox.Enabled = false;
            }
        }

        private void pathPrintSelectButton_Click(object sender, EventArgs e)
        {
            string path = selectPath();
            pathPrintTextBox.Text = path != null ? path : string.Empty;
        }

        private void execProgramPathButton_Click(object sender, EventArgs e)
        {
            string path = selectFile("Вкажіть шлях до програми");
            pathDatabaseTextBox.Text = path != null ? path : string.Empty;

        }
    }
}